package br.com.news.web.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MediaType;

import br.com.news.utils.database.beans.CategoriaBean;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

public class CategoriaService {

	public List<CategoriaBean> categoriasMaisAcessadas() {
		Client c = Client.create();
		WebResource wr = c.resource("http://localhost:8080/news-web-service-rest/Categoria/categoriasMaisAcessadas");
		String json = wr.get(String.class);
		Gson gson = new Gson();
		List<CategoriaBean> categorias = gson.fromJson(json, new TypeToken<ArrayList<CategoriaBean>>() {}.getType());

		return categorias;
	}

	public List<CategoriaBean> findAll() {
		Client c = Client.create();
		WebResource wr = c.resource("http://localhost:8080/news-web-service-rest/Categoria/findAll");
		String json = wr.get(String.class);
		Gson gson = new Gson();
		List<CategoriaBean> categorias = gson.fromJson(json, new TypeToken<ArrayList<CategoriaBean>>() {}.getType());

		return categorias;
	}

	public List<CategoriaBean> categoriasAtivas() {
	    Client c = Client.create();
	    String json = c.resource("http://localhost:8080/news-web-service-rest/Categoria/categoriaAtivas").type(MediaType.APPLICATION_JSON+ ";charset=utf-8").get(String.class);
	    Gson gson = new Gson();
	    List<CategoriaBean> categorias = gson.fromJson(json, new TypeToken<ArrayList<CategoriaBean>>(){}.getType());
	    
		return categorias;
	}
	
	public List<CategoriaBean> findByExample(CategoriaBean categoria) {
		 
		  Gson g = new Gson();
          String json = g.toJson(categoria);

	    Client c = Client.create();
	    String respostaJson = c.resource("http://localhost:8080/news-web-service-rest/Categoria/findByExample").accept(MediaType.APPLICATION_JSON+ ";charset=utf-8").type(MediaType.APPLICATION_JSON+ ";charset=utf-8").post(String.class,json);
	    Gson gson = new Gson();
	    List<CategoriaBean> categorias = gson.fromJson(respostaJson, new TypeToken<ArrayList<CategoriaBean>>(){}.getType());
	    
	    return categorias;
	    
	}
	
	public List<CategoriaBean> findCategoriaByStatus(boolean status) {
		Client c = Client.create();
		WebResource wr = c.resource("http://localhost:8080/news-web-service-rest/Categoria/findCategoriaByStatus/" + status);
		String json = wr.get(String.class);
		Gson gson = new Gson();
		List<CategoriaBean> categorias = gson.fromJson(json, new TypeToken<ArrayList<CategoriaBean>>() {}.getType());

		return categorias;
	}
	
	public void update(CategoriaBean example) {
		 
		  Gson g = new Gson();
		  String json = g.toJson(example);

		    Client c = Client.create();
		    c.resource("http://localhost:8080/news-web-service-rest/Categoria/update").type(MediaType.APPLICATION_JSON+ ";charset=utf-8").put(json);
	 
	}
	
	public void save(CategoriaBean example) {
		 
		  Gson g = new Gson();
		  String json = g.toJson(example);

		    Client c = Client.create();
		    c.resource("http://localhost:8080/news-web-service-rest/Categoria/insert").type(MediaType.APPLICATION_JSON + ";charset=utf-8").put(json);
	 
	}
	
}
